/**
 * @param {character[][]} board
 * @param {string} word
 * @return {boolean}
 */
var exist = function (board, word) {
	//  判空
	if (!board.length) {
		return false;
	}
	// 行列最大值
	let maxRow = board.length, maxCol = board[0].length;
  // 回溯函数
	const dfs = (board, word, row, col, index) => {
		// 判断不符合条件
		if (row < 0 || row >= maxRow || col < 0 || col > maxCol || board[row][col] !== word[index]) {
			return false;
		}
		// word 遍历完了
		if (index === word.length - 1) {
			return true;
		}
		// 记录board的值
		let tmp = board[row][col];
		// 将当前遍历的点锁上，以防向回遍历
		board[row][col] = '-';
		let res = dfs(board, word, row - 1, col, index + 1) || dfs(board, word, row + 1, col, index + 1) || dfs(board, word, row, col - 1, index + 1) || dfs(board, word, row, col + 1, index + 1);
		// 解锁
		board[row][col] = tmp;
		return res;
	}
	// 遍历整个 board ，找到初始位置点
	for (let row = 0; row < maxRow; row++) {
		for (let col = 0; col < maxCol; col++) {
			if (dfs(board, word, row, col, 0)) {
				return true;
			}
		}
	}
	// 没找到
	return false;
};